<template>
    <div class="register-vue" :style="bg">
        <div class="container">
            <p class="title">注册</p>
            <div class="input-c">
                <Input prefix="ios-contact" v-model="username" placeholder="用户名" clearable @on-blur="verifyUsername" />
                <p class="error">{{usernameError}}</p>
            </div>
            <div class="input-c">
                <Input type="password" v-model="password" prefix="md-lock" placeholder="密码" clearable @on-blur="verifyPwd"
                @keyup.enter.native="submit" />
                <p class="error">{{pwdError}}</p>
            </div>
            <div class="input-c">
                <Input type="password" v-model="pwdConfirm" prefix="md-lock" placeholder="密码确认" clearable @on-blur="verifyPwdConfirm"
                       @keyup.enter.native="submit" />
                <p class="error">{{pwdConfirmError}}</p>
            </div>
            <Button :loading="isShowLoading" class="submit" type="primary" @click="submit">注册</Button>
            <p class="account"><span @click="toLogin">去登录</span></p>
        </div>
    </div>
</template>

<script>
export default {
    name: 'register',
    data() {
        return {
            username: 'admin',
            password: '',
            pwdConfirm: '',
            usernameError: '',
            pwdError: '',
            pwdConfirmError: '',
            isShowLoading: false,
            bg: {},
        }
    },
    created() {
        this.bg.backgroundImage = 'url(' + require('../assets/imgs/bg0' + new Date().getDay() + '.jpg') + ')'
    },
    watch: {
        $route: {
            handler(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true,
        },
    },
    methods: {
        verifyUsername() {
            if (!this.username) {
                this.usernameError = '请输入用户名'
            } else {
                this.usernameError = ''
            }
        },
        verifyPwd() {
            if (!this.password) {
                this.pwdError = '请输入密码'
            } else {
                this.pwdError = ''
            }
        },
        verifyPwdConfirm(){
            if (this.password != this.pwdConfirm) {
                this.pwdConfirmError = '两次密码不一致'
            } else {
                this.pwdConfirmError = ''
            }
        },
        // 去登陆
        toLogin() {
            this.$router.push({ path: '/login' })
        },
        async submit() {
            // 注册
           if(this.usernameError || this.pwdError || this.pwdConfirmError){
               this.$Message.error('请检查账号密码!');
               return
           }
           const param = {
               username :this.username,
               password :this.password
           }
           const res = await this.$api.register(param);
           if(res.success){
               this.$Message.success('注册成功!');
               this.$router.push({ path: '/login' })
           }else{
               this.$Message.error(res.message);
           }
        },
    },
}
</script>

<style>
.register-vue {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
}
.register-vue .container {
    background: rgba(255, 255, 255, .5);
    width: 300px;
    text-align: center;
    border-radius: 10px;
    padding: 30px;
}
.register-vue .ivu-input {
    background-color: transparent;
    color: #fff;
    outline: #fff;
    border-color: #fff;
}
.register-vue ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color: rgba(255, 255, 255, .8);
}
.register-vue :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
    color: rgba(255, 255, 255, .8);
}
.register-vue ::-moz-placeholder { /* Mozilla Firefox 19+ */
    color: rgba(255, 255, 255, .8);
}
.register-vue :-ms-input-placeholder { /* Internet Explorer 10-11 */
    color: rgba(255, 255, 255, .8);
}
.register-vue .title {
    font-size: 16px;
    margin-bottom: 20px;
}
.register-vue .input-c {
    margin: auto;
    width: 200px;
}
.register-vue .error {
    color: red;
    text-align: left;
    margin: 5px auto;
    font-size: 12px;
    padding-left: 30px;
    height: 20px;
}
.register-vue .submit {
    width: 200px;
}
.register-vue .account {
    margin-top: 30px;
}
.register-vue .account span {
    cursor: pointer;
}
.register-vue .ivu-icon {
    color: #eee;
}
.register-vue .ivu-icon-ios-close-circle {
    color: #777;
}
</style>
